package com.wen.service.impl;

import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wen.mapper.*;
import com.wen.pojo.*;
import com.wen.utils.PageBean;
import com.wen.utils.Result;
import com.wen.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.wen.service.ProductService;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService{

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public PageBean<Product> allList(Integer pageNum, Integer pageSize, Product product) {
        PageBean<Product> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Product> as = productMapper.allList(product);
        Page<Product> p = (Page<Product>) as;
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    //查询全部仓库
    @Autowired
    private StoreMapper storeMapper;
    @Override
    public List<Store> findStoreList() {
        String store = stringRedisTemplate.opsForValue().get("store");
        if(store == null){
            List<Store> stores = storeMapper.allStoreList();
            stringRedisTemplate.opsForValue().set("store", JSONUtil.toJsonStr(stores));
            return stores;
        }else{
            List<Store> list = JSONUtil.toList(store, Store.class);
            return list;
        }
    }

    //查询全部品牌
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public List<Brand> findBrandList() {
        String brand = stringRedisTemplate.opsForValue().get("brand");
        if(brand == null){
            List<Brand> brands = brandMapper.allBrandList();
            stringRedisTemplate.opsForValue().set("brand", JSONUtil.toJsonStr(brands));
            return brands;
        }else{
            List<Brand> list = JSONUtil.toList(brand, Brand.class);
            return list;
        }
    }
    //查询全部供应商
    @Autowired
    private SupplyMapper supplyMapper;
    @Override
    public List<Supply> allSupplyList() {
        String supply = stringRedisTemplate.opsForValue().get("supply");
        if(supply == null){
            List<Supply> supplies = supplyMapper.allSupply();
            stringRedisTemplate.opsForValue().set("supply", JSONUtil.toJsonStr(supplies));
            return supplies;
        }else{
            List<Supply> list = JSONUtil.toList(supply, Supply.class);
            return list;
        }
    }

    //查询所有单位
    @Autowired
    private UnitMapper unitMapper;
    @Override
    public List<Unit> allUnitList() {
        String unit = stringRedisTemplate.opsForValue().get("unit");
        if(unit == null){
            List<Unit> units = unitMapper.allUnitList();
            stringRedisTemplate.opsForValue().set("unit", JSONUtil.toJsonStr(units));
            return units;
        }else{
            List<Unit> list = JSONUtil.toList(unit, Unit.class);
            return list;
        }
    }

    //查询所有产地
    @Autowired
    private PlaceMapper placeMapper;
    @Override
    public List<Place> allPlaceList() {
        String place = stringRedisTemplate.opsForValue().get("place");
        if(place == null){
            List<Place> places = placeMapper.allPlaceList();
            stringRedisTemplate.opsForValue().set("place", JSONUtil.toJsonStr(places));
            return places;
        }else{
            List<Place> list = JSONUtil.toList(place, Place.class);
            return list;
        }
    }
    //查询所有分类树
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public List<ProductType> productTypeTree() {
        String s = stringRedisTemplate.opsForValue().get("productTypeTree");
        if(s == null){
            //查出所有分类
            List<ProductType> alllist = productTypeMapper.typeList();
            //将所有分类转会为分类树
            List<ProductType> productTypes = allTypeToTypeTree(alllist, 0);

            stringRedisTemplate.opsForValue().set("productTypeTree",JSONUtil.toJsonStr(productTypes));
            return productTypes;
        }else{
            //转为list
            List<ProductType> aList = JSONUtil.toList(s,ProductType.class);
            return aList;
        }
    }
    //添加商品
    @Override
    public Result addProduct(Product product ,String path) {
        String imgPath  = path + product.getImgs();
        CurrentUser currentUser = ThreadLocalUtil.get();
        int userId = currentUser.getUserId();
        Product p = productMapper.findProduct(product.getProductNum());
        if(p == null){
            product.setImgs(imgPath);
            product.setCreateBy(userId);
            int i = productMapper.addProduct(product);
            if(i >0) {
                return Result.ok(200,"添加成功",null);
            }else{
                return Result.fail(1,"添加失败");
            }
        }else{
            return Result.fail(200,"重复，失败");
        }

    }

    //修改上下架状态
    @Override
    public Result updState(Product product) {
        int i =  productMapper.updState(product);
        if(i >0) {
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }

    //批量删除商品
    @Override
    public Result delAllProduct(Integer[] ids) {
        int i = productMapper.delAllProduct(ids);
        if(i >0) {
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }
    //修改商品
    @Override
    public Result updProduct(Product product,String path) {
        Product p = productMapper.findProduct(product.getProductNum());
        if(p == null ){
            String imgpath =  path + product.getImgs();
            CurrentUser currentUser = ThreadLocalUtil.get();
            int userId = currentUser.getUserId();
            product.setUpdateBy(userId);
            product.setImgs(imgpath);
            int i  = productMapper.updProduct(product);
            if(i >0) {
                return Result.ok(200,"成功",null);
            }else{
                return Result.fail(1,"失败");
            }
        }else{
            return Result.fail(1,"编号重复，失败");
        }

    }

    //分类树递归算法
    private List<ProductType> allTypeToTypeTree(List<ProductType> alllist,Integer pid){
        //一级菜单
        List<ProductType> firstLevelType =new ArrayList<>();
        for (ProductType productType : alllist){
            if(productType.getParentId().equals(pid)){
                firstLevelType.add(productType);
            }
        }
        //拿到每一个一级菜单中所有的二级菜单
        for (ProductType productType: firstLevelType){
            List<ProductType> twoLevelType = allTypeToTypeTree(alllist, productType.getTypeId());
            productType.setChildProductCategory(twoLevelType);
        }
        return firstLevelType;
    }
}
